# Maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=llvmlite
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.37.0
pkgrel=1
pkgdesc='Lightweight LLVM python binding for writing JIT compilers (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/numba/llvmlite"
license=('BSD')
depends=(
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-polly>=10.0.1")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-mlir"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('!emptydirs' 'staticlibs' 'strip')
source=(llvmlite-$pkgver.tar.gz::"https://github.com/numba/llvmlite/archive/v$pkgver.tar.gz"
       "multi-defs.patch"
       "path_fix.patch"
       "llvm-12.0.patch")
sha256sums=('7405f4f1a9898f3e408bc82a7dd67b6bdb0dd47d203e416dfe09a1ec976eb04b'
            'b4610934ac8fd7e614d9ea920856ff6da2fbeb146028a664ada8543b8b33ec56'
            '813ecc48f18543f0d36b03c7596a1a6a26be31b9cfa44f1111ac232821844a79'
            'ff31146ec66374b77d37528c13178e73bc8e92227b8919ed715963cc81bf5ed9')

prepare() {  
  cd "$srcdir/${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}/multi-defs.patch"
  patch -p1 -i "${srcdir}/path_fix.patch"

  patch -p1 -i "${srcdir}/llvm-12.0.patch"
  sed -i "s,_MSC_VER,_WIN32," ffi/core.h
}

build() {
  cd "$srcdir"
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"

  msg "Python build for ${CARCH}"
  export CMAKE_GENERATOR="MSYS Makefiles"

  # Workaround from https://github.com/AcademySoftwareFoundation/OpenShadingLanguage/issues/1069
  CXXFLAGS+=" -DNDEBUG" \
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  export CMAKE_GENERATOR="MSYS Makefiles"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
  install -Dm644 ffi/build/libllvmlite.dll "${pkgdir}${MINGW_PREFIX}/bin/llvmlite.dll"
  install -Dm644 ffi/build/libllvmlite.dll.a "${pkgdir}${MINGW_PREFIX}/lib/libllvmlite.dll.a"
}
